package io.renren.modules.gengyun.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.gengyun.entity.SoilConditionHourlyEntity;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.gengyun.entity.AirConditionHourlyEntity;
import io.renren.modules.gengyun.service.AirConditionHourlyService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author krlin
 * @email 2032153705@qq.com
 * @date 2020-09-17 20:31:48
 */
@Api("空气情况获取接口")
@RestController
@RequestMapping("gengyun/airconditionhourly")
public class AirConditionHourlyController {
    @Autowired

    private AirConditionHourlyService airConditionHourlyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("gengyun:airconditionhourly:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = airConditionHourlyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gengyun:airconditionhourly:info")
    public R info(@PathVariable("id") Long id) {
        AirConditionHourlyEntity airConditionHourly = airConditionHourlyService.getById(id);

        return R.ok().put("airConditionHourly", airConditionHourly);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gengyun:airconditionhourly:save")
    public R save(@RequestBody AirConditionHourlyEntity airConditionHourly) {
        airConditionHourlyService.save(airConditionHourly);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gengyun:airconditionhourly:update")
    public R update(@RequestBody AirConditionHourlyEntity airConditionHourly) {
        airConditionHourlyService.updateById(airConditionHourly);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("gengyun:airconditionhourly:delete")
    public R delete(@RequestBody Long[] ids) {
        airConditionHourlyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取某一地块的空气数据
     *
     * @param params
     * @return
     */
    @RequestMapping("/listById")
    @RequiresPermissions("gengyun:airconditionhourly:list")
    public R listById(@RequestParam Map<String, Object> params) {

        int pageIndex = Integer.parseInt((String) params.get("page"));
        int pageSize = Integer.parseInt((String) params.get("limit"));
        String cropRotationId = (String) params.get("key");

        System.out.println(pageIndex);
        System.out.println(pageSize);
        System.out.println(cropRotationId);

        Page<AirConditionHourlyEntity> page = new Page<>(pageIndex, pageSize);

        Page<AirConditionHourlyEntity> list = airConditionHourlyService.query()
                .eq("crop_rotation_id", cropRotationId)
                .page(page);
        // 用PageUtils做格式化
        PageUtils pages = new PageUtils(list);
        return R.ok().put("page", pages);
    }


//    /**
//     * 根据地块 id , 开始时间与结束时间查询
//     *
//     * @param params
//     * @return
//     */
//    @RequestMapping("/listByIdAndTime")
//    @RequiresPermissions("gengyun:airconditionhourly:list")
//    public R listByIdAndTime(@RequestParam Map<String, Object> params) {
//
//        String cropRotationId = (String) params.get("key");
//        String beginTime = (String) params.get("beginTime");
//        String endTime = (String) params.get("endTime");
//
//        List<AirConditionHourlyEntity> list = airConditionHourlyService.query()
//                .eq("crop_rotation_id", cropRotationId)
//                .between("create_timestamp", Long.parseLong(beginTime), Long.parseLong(endTime))
//                .list();
//        // 处理时间
//        List<String> timestamps = new ArrayList<>();
//        // 处理温度
//        List<Double> temperatures = new ArrayList<>();
//        // 处理湿度
//        List<Double> humidities = new ArrayList<>();
//        // 降雨量
//        List<Double> precipitations = new ArrayList<>();
//        list.forEach( x-> {
//            timestamps.add(x.getFormatTime());
//            temperatures.add(x.getTemperatureValue() / 100.0);
//            humidities.add(x.getHumidityValue() / 100.0);
//            precipitations.add(x.getPrecipitationValue() / 1.0);
//        });
//        return R.ok().put("timestamps",timestamps)
//                .put("temperatures", temperatures)
//                .put("humidities", humidities)
//                .put("precipitations", precipitations);
//    }
}
